package org.flower.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.session.SqlSession;
import org.flower.bean.CommodityDetails;
import org.flower.bean.CommodityDetailsExample;
import org.flower.mapper.CommodityDetailsMapper;
import org.flower.service.CommodityDetailsService;
import org.flower.utils.MyBatisUtil;

import java.io.IOException;
import java.util.List;

/**
 * @author Aizen
 */
public class CommodityDetailsServiceImpl implements CommodityDetailsService {
    @Override
    public PageInfo<CommodityDetails> findAll(CommodityDetails com, int startRow) throws IOException {
        SqlSession sqlSessionFactory = MyBatisUtil.getSqlSessionFactory();
        CommodityDetailsMapper mapper = sqlSessionFactory.getMapper(CommodityDetailsMapper.class);

        //创建模糊查询条件对象
        CommodityDetailsExample commodityDetailsExample = new CommodityDetailsExample();
        CommodityDetailsExample.Criteria criteria = commodityDetailsExample.createCriteria();
        //名称
        if(com.getComname()!=null&&!com.getComname().equals("")){
            criteria.andComnameLike("%"+com.getComname()+"%");
        }
        //类别
        if(com.getClassid()!=null&&!com.getClassid().equals("")&&!com.getClassid().equals(0)){
            criteria.andClassidEqualTo(com.getClassid());
        }
        //价格区间
        if(com.getOutPriceMin()!=null&&!com.getOutPriceMin().equals("")){
            if(com.getOutPriceMax()!=null&&!com.getOutPriceMax().equals("")){
                criteria.andOutpriceBetween(com.getOutPriceMin(),com.getOutPriceMax());
            }else{
                criteria.andOutpriceBetween(com.getOutPriceMin(),9999999999.0);
            }
        }else if(com.getOutPriceMax()!=null&&!com.getOutPriceMax().equals("")){
            criteria.andOutpriceBetween(0.0,com.getOutPriceMax());
        }

        //每页显示的商品数量，默认是10
        int pageSize = 10;
        //如果是商品搜索页面就显示20个
        if(com.isFindFlag()){
            pageSize = 20;
        }

        //判断查询排序
        Integer sortNo = com.getSortNo();
        switch (sortNo){
            case 1:
                commodityDetailsExample.setOrderByClause("outNum desc");
                break;
            case 2:
                commodityDetailsExample.setOrderByClause("outPrice desc");
                break;
            case 3:
                commodityDetailsExample.setOrderByClause("outPrice");
                break;
            case 4:
                commodityDetailsExample.setOrderByClause("insertDate desc");
                break;
            default:
                break;
        }


        PageHelper.startPage(startRow, pageSize);
        List<CommodityDetails> commodityDetails = mapper.selectByExample(commodityDetailsExample);
        PageInfo<CommodityDetails> pageInfo = new PageInfo<CommodityDetails>(commodityDetails,10);
        sqlSessionFactory.close();
        return pageInfo;
    }

    @Override
    public PageInfo<CommodityDetails> findByPid(int staNum,int pid) {
        SqlSession sqlSessionFactory = null;
        try {
            sqlSessionFactory = MyBatisUtil.getSqlSessionFactory();
        } catch (IOException e) {
            e.printStackTrace();
        }
        CommodityDetailsMapper mapper = sqlSessionFactory.getMapper(CommodityDetailsMapper.class);
        PageHelper.startPage(staNum,10);
        List<CommodityDetails> byDiy = mapper.findByDiy(pid);
        PageInfo<CommodityDetails> pageInfo = new PageInfo<>(byDiy, 10);
        sqlSessionFactory.close();
        return pageInfo;
    }

    @Override
    public boolean addCommodity(CommodityDetails commodityDetails) {
        return false;
    }



}

























